//
//  UserGiftCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/1.
//

import UIKit

class UserGiftCell: UITableViewCell {
    var bkgView: UIView!
    var imgView: UIImageView!
    var titleLab: UILabel!
    var detailLab: UILabel!
    var pointLab: UILabel!
    var bottomBtn: UIButton!
    var data: IntegralProductDataContentModel!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        bkgView = UIView()
        bkgView.backgroundColor = .white
        contentView.addSubview(bkgView)
        
        imgView = UIImageView()
        imgView.backgroundColor = Theme.lineColor
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        bkgView.addSubview(imgView)
        
        titleLab = UILabel()
        titleLab.textColor = Theme.titleLabColor
        titleLab.backgroundColor = UIColor.clear
        titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        titleLab.numberOfLines = 0
        titleLab.lineBreakMode = .byTruncatingTail
        titleLab.textAlignment = .left
        bkgView.addSubview(titleLab)
        
        detailLab = UILabel()
        detailLab.textColor = Theme.titleLabColor
        detailLab.backgroundColor = UIColor.clear
        detailLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        detailLab.numberOfLines = 0
        detailLab.lineBreakMode = .byTruncatingTail
        detailLab.textAlignment = .left
        bkgView.addSubview(detailLab)
        
        pointLab = UILabel()
        pointLab.textColor = Theme.titleLabColor
        pointLab.backgroundColor = UIColor.clear
        pointLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 12)
        pointLab.numberOfLines = 1
        pointLab.lineBreakMode = .byTruncatingTail
        pointLab.textAlignment = .left
        bkgView.addSubview(pointLab)
        
        bottomBtn = UIButton()
        bottomBtn.setup_titleLabel(text: MMLocalizedString("exchange_now", comment: "立即兑换"))
        bottomBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        bottomBtn.setup_titleColor(color: Theme.whiteColor)
        bottomBtn.setBackgroundImage(UIImage.init(color: Theme.mainColor), for: .normal)
        bottomBtn.setBackgroundImage(UIImage.init(color: Theme.mainColorHighLightColor), for: .normal)
        bottomBtn.layer.cornerRadius = 8
        bottomBtn.layer.masksToBounds = true
        bottomBtn.addTarget(self, action: #selector(bottomBtnAction), for: .touchUpInside)
        bkgView.addSubview(bottomBtn)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(0)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(128)
            make.bottom.equalTo(-10).priority(.high)
        }
        
        imgView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(14)
            make.width.equalTo(100)
            make.height.equalTo(100)
            make.bottom.lessThanOrEqualTo(-14)
        }
        
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(14)
            make.height.greaterThanOrEqualTo(20)
            make.right.equalTo(-10)
        }
        
        detailLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(titleLab.snp.bottom).offset(4)
            make.right.equalTo(-10)
        }
        
        pointLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.greaterThanOrEqualTo(detailLab.snp.bottom).offset(10)
            make.height.equalTo(22)
            make.bottom.equalTo(-10)
        }
        
        bottomBtn.titleLabel?.frame.size.width = 200
        bottomBtn.titleLabel?.sizeToFit()
        let btn_width = bottomBtn.titleLabel?.frame.size.width ?? 0
        bottomBtn.snp.makeConstraints { make in
            make.left.equalTo(pointLab.snp.right)
            make.right.equalTo(-10)
//            make.top.equalTo(detailLab.snp.bottom).offset(10)
            make.height.equalTo(28)
            make.width.equalTo(btn_width + 32)
//            make.bottom.lessThanOrEqualTo(-16)
            make.centerY.equalTo(pointLab.snp.centerY)
        }
        
        self.layoutIfNeeded()
        self.createShadowLayer()
    }
    
    private func createShadowLayer(){
        bkgView.layer.masksToBounds = false
        //设置边框圆角
        bkgView.layer.cornerRadius = 8
        //设置阴影颜色
        bkgView.layer.shadowColor = UIColor.init(hex: 0xF4F4F9, alpha: 0.9).cgColor
        //设置透明度
        bkgView.layer.shadowOpacity = 1
        //设置阴影半径
        bkgView.layer.shadowRadius = 10
        //设置阴影偏移量
        bkgView.layer.shadowOffset = CGSize(width: 0, height: 0)
    }
    
    func setUserGiftCell(data: IntegralProductDataContentModel? = nil) {
        self.data = data
        if let url = URL.init(string: data?.urlPicture ?? "") {
            self.imgView.kf.setImage(with: url, placeholder: UIImage.init(named: "icon_default_place_v"))
        }else{
            self.imgView.image = UIImage.init(named: "icon_default_place_v")
        }
        self.titleLab.text = data?.integralName ?? ""
        self.detailLab.text = data?.content ?? ""
        self.pointLab.text = String(format: "%.0f %@", data?.integralValue ?? 0, MMLocalizedString("points", comment: "积分"))
        self.updateConstraints()
    }
    
    ///立即兑换
    @objc func bottomBtnAction(){
        self.data.exchangeNowBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
